package xin.marcher.module.seller.converter;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
public class ListMapper {

    /**
     * list 转 string
     *
     * @param src
     * @return
     */
    public static String list2Str(List<Integer> src) {
        if (CollectionUtils.isEmpty(src)) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        src.forEach(item -> sb.append(item).append(","));
        return sb.substring(0, sb.length() - 1);
    }

    /**
     * string 转 list
     *
     * @param src
     * @return
     */
    public static List<Integer> str2List(String src) {
        if (StringUtils.isEmpty(src)) {
            return Collections.emptyList();
        }

        String[] split = src.split(",");
        List<String> result = Arrays.asList(split);
        return result.stream().map(Integer::parseInt).collect(Collectors.toList());
    }
}
